package com.java.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.java.dao.TodoDao;
import com.java.entity.TodoList;
import com.java.ext.MyButton;
import com.java.utils.MyTools;
import com.java.utils.MyWindowListener;  

/**
* @ClassName: AddTodoWindow
* @Description: 添加待办界面
*
 */
public class AddTodoWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 父级窗口
	 */
	JFrame pJFrame;
	
	private TodoList selectObj = null;

	// 构造函数  
    public AddTodoWindow(JFrame bJFrame, TodoList todo) {
    	pJFrame = bJFrame;
    	selectObj = todo;
        // 设置窗口属性  
        setTitle(null == todo?"Add to-do list":"Modify to-do list");  
		setSize(350, 270);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  
        addWindowListener(new CloseButtonListener()); 
        setLocationRelativeTo(null);
  
        // 创建用于布局的 JPanel  
        JPanel contentPane = new JPanel(); 
        contentPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));  
        contentPane.setLayout(new GridLayout(7, 2)); // 使用网格布局管理器  
  
        // 创建标题输入框  
        final JTextField titleField = new JTextField(60);
        titleField.setText(todo != null ?todo.getTitle(): "");
        contentPane.add(new JLabel("Title: "), new GridBagConstraints());  
        contentPane.add(titleField, new GridBagConstraints());  
        
        // 创建优先级输入框  
        final JComboBox<String> levelBox = new JComboBox<>(new String[]{"Important", "Special", "Ordinary", "Daily"});  
        contentPane.add(new JLabel("Priority: "), new GridBagConstraints());  
        levelBox.setSelectedItem(todo != null ?todo.getLevel(): "");  
        contentPane.add(levelBox, new GridBagConstraints());  
  
        // 创建开始输入框  
        final JTextField startField = new JTextField(120);  
        contentPane.add(new JLabel("Start Datetime: "), new GridBagConstraints());  
        startField.setText(todo != null ?String.valueOf(todo.getStartTime()): MyTools.getNowDate());
        contentPane.add(startField, new GridBagConstraints());  
  
        // 创建结束输入框  
        final JTextField endField = new JTextField(120);  
        contentPane.add(new JLabel("Finish Datetime: "), new GridBagConstraints());  
        endField.setText(todo != null ?todo.getEndTime(): MyTools.getNowDate());
        contentPane.add(endField, new GridBagConstraints());  
  
        // 创建重置按钮和提交按钮的面板  
        JPanel buttonPanel = new JPanel();  
        buttonPanel.setLayout(new FlowLayout()); // 使用流式布局，使按钮水平排列
        MyButton returnBtn = new MyButton("GoBack").defaultt();
        buttonPanel.add(returnBtn);  
        MyButton saveBtn = new MyButton("Submit").primary();
        buttonPanel.add(saveBtn);  
  
        // 将按钮面板添加到主窗口的底部  
        getContentPane().add(contentPane, BorderLayout.CENTER);  
        getContentPane().add(buttonPanel, BorderLayout.SOUTH); // 添加到底部 
		
        returnBtn.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
            	setVisible(false);
            	pJFrame.setEnabled(true);
            }  
        }); 
		
        saveBtn.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
            	String title = titleField.getText();
            	Object level = levelBox.getSelectedItem();
            	String start = startField.getText();
            	String end = endField.getText();
            	if("".equals(title)) {
            		warning("Input title...");
            		return;
            	}
            	if("".equals(level)) {
            		warning("Selected priority");
            		return;
            	}
            	if("".equals(start)) {
            		warning("Input start...");
            		return;
            	}
            	Date startDate, endDate;
            	boolean isStart = MyTools.isLegalDate(10, start, MyTools.Date);
            	if(!isStart) {
            		isStart = MyTools.isLegalDate(19, start, MyTools.DateTime);
            		if(!isStart) {
            			warning("Date/time format error");
                		return;
            		}else {
                		startDate = MyTools.formatDateTime(start);
                		start = MyTools.date2Str(startDate, MyTools.DateTime);
                	}
            	}else {
            		startDate = MyTools.formatDate(start);
            		start = MyTools.date2Str(startDate, MyTools.Date);
            	}
            	if("".equals(end)) {
            		warning("Input finish...");
            		return;
            	}
            	boolean isEnd = MyTools.isLegalDate(10, end, MyTools.Date);
            	if(!isEnd) {
            		isEnd = MyTools.isLegalDate(19, end, MyTools.DateTime);
            		if(!isEnd) {
            			warning("Date/time format error");
                		return;
            		}else {
                		endDate = MyTools.formatDateTime(end);
                		end = MyTools.date2Str(endDate, MyTools.DateTime);
                	}
            	}else {
            		endDate = MyTools.formatDate(end);
            		end = MyTools.date2Str(endDate, MyTools.Date);
            	}
            	if(start.length() != end.length()) {
            		warning("The start and end time formats do not correspond");
            		return;
            	}
            	if(endDate.getTime() - startDate.getTime() < 0) {
            		warning("The start time cannot be less than the end time");
            		return;
            	}
            	if(null == selectObj) {
            		selectObj = new TodoList();
            		selectObj.setTitle(title);
            		selectObj.setLevel(level.toString());
            		selectObj.setStartTime(start);
            		selectObj.setEndTime(end);
            		TodoDao.saveTodo(selectObj);
            	}else {
            		selectObj.setTitle(title);
            		selectObj.setLevel(level.toString());
            		selectObj.setStartTime(start);
            		selectObj.setEndTime(end);
            		TodoDao.updateTodo(selectObj);
            	}
            	ok("Success");
                setVisible(false);
                pJFrame.dispose();
            	new AdminMainWindow();
            }  
        }); 
        
		setVisible(true);
		
    } 
    
    class CloseButtonListener extends MyWindowListener {
    	
    	@Override
    	public void windowClosing(WindowEvent e) {  
        	
        }
    }
      
}